home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / ssf.lha / src / SSF_Main.c < prev    next >
C/C++ Source or Header  |  1995-11-15  |  7KB  |  275 lines

  1. /* ------------------------------------------------------------------------ */
  2. /*                      SIM System-Folder (shell Client)                    */
  3. /*                          ---------------------                           */
  4. /*  © by Volker Graf in 1995. This Program is distributed under the terms   */
  5. /*  of FREEWARE                                                             */
  6. /* ------------------------------------------------------------------------ */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #define FILEENDINGS "s:FileEndings"
  13.  
  14. /* /// Prototypes & Global Variables */
  15.  
  16. /* ##################### Function Prototypes ####################### */
  17. int ScanDataBase (void);
  18. int AllocateMemory (void);
  19. int ReadDataBase(void);
  20. void CleanUp (void);
  21. /* ################################################################# */
  22.  
  23. /* #####################  Global variables  ######################## */
  24. char **FileEndings, **Comments, **DestPath;
  25. int MAXLINES, MAXCOMMENT, MAXDESTPATH;  /* Values for File analysis */
  26. /* ################################################################# */
  27. /* /// */
  28.  
  29. /* ///ScanDataBase*************************************** */
  30. int ScanDataBase ()
  31. /*      FUNCTION:   Analyse the DataBase-File
  32.  *      PARAMETERS:
  33.  *         INPUT: -
  34.  *         OUTPUT: 0 (O.K.)  -1 (FAIL)
  35.  * ****************************************************** */
  36. {
  37.    FILE *FP;
  38.    char c, DUMMY[1000];         /* Source of ERROR !!!! */
  39.    int comment, ocomment, destpath, odestpath, lines;
  40.  
  41.    comment = destpath = lines = 0;
  42.  
  43.    FP = fopen (FILEENDINGS, "r");
  44.  
  45.    if (FP == NULL)
  46.       return (-1);              /* ERROR Could NOT open Database */
  47.  
  48.    while ((fscanf (FP, "%s", &DUMMY)) != EOF) {         /* Analyse the DataBase File */
  49.       lines++;                  /* Lines *//* lines will be the # of entries */
  50.  
  51.       ocomment = odestpath = 0;
  52.  
  53.       while ((c = fgetc (FP)) != EOF && (c != '#'))     /* comment will be the max length of */
  54.          ocomment++;            /*   chars for the comment line */
  55.       if (ocomment > comment)
  56.          comment = ocomment;
  57.       while ((c = fgetc (FP)) != EOF && (c != '\n'))    /* destpath will be the max. length of */
  58.          odestpath++;           /*   chars of the dest. path description */
  59.       if (odestpath > destpath)
  60.          destpath = odestpath;
  61.    }
  62.    MAXLINES = lines;
  63.    MAXCOMMENT = comment;
  64.    MAXDESTPATH = destpath;
  65.  
  66.    fclose (FP);
  67. }
  68. /* /// */
  69.  
  70. /* /// AllocateMemory************************************* */
  71. int AllocateMemory ()
  72. {
  73. /*      FUNCTION: Allocates Memory for the DataBase entries
  74.  *      PARAMETERS:
  75.  *         INPUT:  -
  76.  *         OUTPUT: 0 (O.K.) -1 (FAIL)
  77.  * ******************************************************** */
  78.  
  79.    int i;
  80.  
  81.    FileEndings = (char **) malloc (MAXLINES * sizeof (char *));
  82.    if (FileEndings == NULL)
  83.       return (-1);
  84.  
  85.    for (i = 0; i < MAXLINES; i++) {
  86.       FileEndings[i] = (char *) malloc (5 * sizeof (char));
  87.       if (FileEndings[i] == NULL)
  88.          return (-1);
  89.    }                            /* End FOR */
  90.  
  91.    Comments = (char **) malloc (MAXLINES * sizeof (char *));
  92.    if (Comments == NULL)
  93.       return (-1);
  94.  
  95.    for (i = 0; i < MAXLINES; i++) {
  96.       Comments[i] = (char *) malloc (MAXCOMMENT * sizeof (char));
  97.       if (Comments[i] == NULL)
  98.          return (-1);
  99.    }                            /* End FOR */
  100.  
  101.    DestPath = (char **) malloc (MAXLINES * sizeof (char *));
  102.    if (DestPath == NULL)
  103.       return (-1);
  104.  
  105.    for (i = 0; i < MAXLINES; i++) {
  106.       DestPath[i] = (char *) malloc (MAXDESTPATH * sizeof (char));
  107.       if (DestPath[i] == NULL)
  108.          return (-1);
  109.    }                            /* End FOR */
  110. }
  111.  
  112. /* /// */
  113.  
  114. /* ///CleanUp******************************************** */
  115. void CleanUp ()
  116. {
  117. /*  FUNCTION: Cleans up all the allocated memory
  118.  *  PARAMETERS:
  119.  *     INPUT: -
  120.  *     OUTPUT: -
  121.  * ************************************************ */
  122.  
  123.    int i;
  124.  
  125.    if (FileEndings) {
  126.       for (i = 0; i < MAXLINES; i++) {
  127.          if (FileEndings[i])
  128.             free (FileEndings[i]);
  129.       }
  130.       free (FileEndings);
  131.    }
  132.  
  133.    if (Comments) {
  134.       for (i = 0; i < MAXLINES; i++) {
  135.          if (Comments[i])
  136.             free (Comments[i]);
  137.       }
  138.       free (Comments);
  139.    }
  140.  
  141.    if (DestPath) {
  142.       for (i = 0; i < MAXLINES; i++) {
  143.          if (DestPath[i])
  144.             free (DestPath[i]);
  145.       }
  146.       free (DestPath);
  147.    }
  148. }
  149. /* /// */
  150.  
  151. /* ///ReadDataBase*************************************** */
  152. int ReadDataBase ()
  153. {
  154. /*  FUNCTION: Read the DataBase and assign data to "FileEndings", "Comments" and "DestPath"
  155.  *  PARAMETERS:
  156.  *     INPUT:  -   (e.g. later FileName)
  157.  *     OUTPUT: 0 (O.K.)    -1 (FAIL)
  158.  * ************************************************** */
  159.    FILE *FP;
  160.    int i, x;
  161.  
  162.    i = x = 0;
  163.    FP = fopen (FILEENDINGS, "r");
  164.  
  165.    if (FP == NULL)
  166.       return (-1);
  167.  
  168.    while ((fscanf (FP, "%s", FileEndings[i])) != EOF) {
  169.       while (((Comments[i][x] = fgetc (FP)) != EOF) && (Comments[i][x] != '#'))
  170.          x++;
  171.       Comments[i][x]= '\0';
  172.       x = 0;
  173.       while (((DestPath[i][x] = fgetc (FP)) != EOF) && (DestPath[i][x] != '\n'))
  174.          x++;
  175.       DestPath[i][x]= '\0';
  176.       x = 0;
  177.       i++;
  178.    }
  179.   fclose (FP);
  180. }
  181. /* /// */
  182.  
  183. /* ///Analyze******************************************** */
  184. int Analyze (char *INPUT){
  185. /*  FUNCTION: Read the Input and check for Ending
  186.         PARAMETERS:
  187.             INPUT: char *INPUT
  188.             OUTPUT: 0(O.K.)  -1(FAIL)
  189.     ***************************************************** */
  190.     int x,y,i,z;
  191.     char *STRING, COMMANDSTRING[1000];  /* ERROR SOURCE !!!!!! */
  192.  
  193.  
  194.     i = z = 0;
  195.     x = strlen(INPUT);
  196.     y = x;
  197.  
  198.     STRING = (char *) malloc (x * sizeof(char));
  199.  
  200.  
  201.     x--; /* Last Letter */
  202.  
  203.     while((INPUT[x] != '.') && (x >= 0))
  204.         x--;
  205.     if (x==0)   /* is there a '.' in the Filename ? */
  206.         return(-1);
  207.  
  208.     for (i=x;i<y;i++)
  209.         STRING[z++]=INPUT[i];
  210.     STRING[z++] = '\0';
  211.     /* Now we created the ENDING .. now let's look if we have it in our DataBase */
  212.  
  213.     for (i=0;i<MAXLINES;i++){
  214.         if ((strcmp(STRING,FileEndings[i])) == 0){   /* An Entry an Entry an Entry !!!! */
  215.             strcpy(COMMANDSTRING,"copy \"");
  216.             strcat(COMMANDSTRING,INPUT);
  217.             strcat(COMMANDSTRING,"\" \"");
  218.             strcat(COMMANDSTRING,DestPath[i]);
  219.             strcat(COMMANDSTRING,"\"");
  220.             system(COMMANDSTRING);
  221.             }
  222.     }
  223.  
  224.  
  225. }
  226. /* /// */
  227.  
  228.  
  229. /* ################################################################################
  230.  * 
  231.  * 
  232.  * 
  233.  *                                    M A I N
  234.  * 
  235.  * 
  236.  * 
  237.  * ################################################################################ */
  238.  
  239. main (int argc, char *argv[])
  240. {
  241.  
  242.    int i, res;
  243.  
  244.    res = ScanDataBase ();
  245.  
  246.    if (res == -1) {             /* FAIL */
  247.       fprintf (stderr, "Could NOT open Database File.. exiting ..\n");
  248.       exit (1);
  249.    }
  250.  
  251.    res = AllocateMemory ();
  252.  
  253.    if (res == -1) {             /* FAIL */
  254.       CleanUp ();
  255.       exit (1);
  256.    }
  257.    /* Memory Allocated */
  258.    res = ReadDataBase ();
  259.  
  260.    if (res == -1) {
  261.       CleanUp ();
  262.       exit (1);
  263.    }
  264.  
  265.    for (i=0; i < MAXLINES; i++)
  266.         fprintf(stderr," %s\t %s\t %s\t\n",FileEndings[i], Comments[i], DestPath[i]);
  267.  
  268.    res = Analyze(argv[1]);
  269.  
  270.  
  271.    CleanUp();
  272.  
  273. }
  274.  
  275.